package com.tmxk.employee.dao.impl;

import com.tmxk.employee.dao.TScheduleDao;
import com.tmxk.employee.pojo.entity.TSchedule;
import com.tmxk.employee.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TScheduleDaoImpl extends BaseDao implements TScheduleDao {
    @Override
    public List<TSchedule> queryAllSchedule(Map<String, Object> map) {
        List<TSchedule> scheduleList = new ArrayList<>();
        TSchedule schedule = null;
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ID,USER_NAME,DUTY,TRIP_MODE_ID,STATUS," +
                "TO_CHAR(DEPART_TIME,'yyyy-MM-dd') DEPART_TIME FROM T_SCHEDULE ");
        sql.append(" WHERE 1 = 1 ");
        if (map != null && map.size() > 0) {
            Object tripModeId = map.get("tripModeId");
            Object tripTime = map.get("tripTime");
            Object tripStatus = map.get("status");
            //用户只查询条件1
            if (tripModeId != null && tripTime == null && tripStatus == null) {
                sql.append("AND  TRIP_MODE_ID = ? ");
                rs = super.executeQuery(sql.toString(), new Object[]{map.get("tripModeId")});
            }
            //用户只查询条件2
            if (tripTime != null && tripModeId == null && tripStatus == null) {
                sql.append(" AND DEPART_TIME = TO_DATE(?,'yyyy-MM-dd')  ");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(), new Object[]{tripTime});
            }

            //  用户查询条件1 +  查询条件2 (组合查询)
            if (tripModeId != null && tripTime != null && tripStatus == null) {
                sql.append(" AND  TRIP_MODE_ID = ? ");
                sql.append(" AND DEPART_TIME = TO_DATE(?,'yyyy-MM-dd') ");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(), new Object[]{tripModeId, tripTime});
            }
            //1+3
            if (tripTime == null && tripModeId != null && tripStatus != null) {
                sql.append(" AND  TRIP_MODE_ID = ? ");
                sql.append("AND STATUS = ?");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(), new Object[]{tripModeId, tripStatus});
            }
            //2+3
            if (tripModeId == null && tripTime != null && tripStatus != null) {
                sql.append(" AND DEPART_TIME = TO_DATE(?,'yyyy-MM-dd') ");
                sql.append("AND STATUS = ?");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(), new Object[]{tripTime, tripStatus});
            }
            //1+2+3
            if (tripModeId != null && tripTime != null && tripStatus != null) {
                sql.append(" AND  TRIP_MODE_ID = ? ");
                sql.append(" AND DEPART_TIME = TO_DATE(?,'yyyy-MM-dd') ");
                sql.append("AND STATUS = ?");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(), new Object[]{tripModeId,tripTime, tripStatus});
            }
            //条件查询3
            if (tripStatus != null && tripTime == null && tripModeId == null) {
                sql.append("AND STATUS = ?");
                rs = super.executeQuery(sql.toString(), new Object[]{tripStatus});
            }
            // userName
            if( map.get("userName") != null){
                sql.append(" AND USER_NAME = ?  ");
                rs = super.executeQuery(sql.toString(),new Object[]{ map.get("userName")});
            }
        } else {
            //查询所有
            //调用公共的查询方法
            rs= super.executeQuery(sql.toString());
        }
        try {
            while (rs.next()) {
                schedule = new TSchedule();
                schedule.setId(rs.getInt("ID"));
                schedule.setUserName(rs.getString("USER_NAME"));
                schedule.setDuty(rs.getString("DUTY"));
                schedule.setTripModeId(rs.getInt("TRIP_MODE_ID"));
                schedule.setStatus(rs.getInt("STATUS"));
                schedule.setDepartTime(rs.getString("DEPART_TIME"));
                scheduleList.add(schedule);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAllConn(conn, pstmt, rs);
        }
        return scheduleList;
    }

    @Override
    public int addSchedule(TSchedule schedule) {
        String sql = "INSERT INTO T_SCHEDULE(ID,USER_NAME,DUTY,TRIP_MODE_ID,STATUS,DEPART_TIME) " +
                "VALUES(SEQ_SCHEDULE_ID.NEXTVAL,?,?,?,0,TO_DATE(?,'yyyy-MM-dd'))" ;
        return super.executeUpdate(sql ,
                new Object[]{schedule.getUserName(),schedule.getDuty(),
                            schedule.getTripModeId(),schedule.getDepartTime()});
    }

    @Override
    public int update(String id, String status) {
        String sql = "update T_SCHEDULE set STATUS = ? where ID = ?";
        return super.executeUpdate(sql,new Object[]{status,id});
    }

    @Override
    public int deleteSchedule(String id) {
        return executeUpdate("delete from T_SCHEDULE where ID = ? " ,new Object[]{id});
    }
}
